package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.system.domain.Product;
import com.ruoyi.system.domain.Repertory;
import com.ruoyi.system.mapper.ProductMapper;
import com.ruoyi.system.mapper.RepertoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.StockMapper;
import com.ruoyi.system.domain.Stock;
import com.ruoyi.system.service.IStockService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 进货信息Service业务层处理
 * 
 * @author 徐国洋
 * @date 2025-02-24
 */
@Service
public class StockServiceImpl implements IStockService 
{
    @Autowired
    private StockMapper stockMapper;

    @Autowired
    private RepertoryMapper repertoryMapper;

    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询进货信息
     * 
     * @param id 进货信息主键
     * @return 进货信息
     */
    @Override
    public Stock selectStockById(Long id)
    {
        return stockMapper.selectStockById(id);
    }

    /**
     * 查询进货信息列表
     * 
     * @param stock 进货信息
     * @return 进货信息
     */
    @Override
    public List<Stock> selectStockList(Stock stock)
    {
        return stockMapper.selectStockList(stock);
    }

    /**
     * 新增进货信息
     * 
     * @param stock 进货信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertStock(Stock stock)
    {
        //添加进货记录
       int result =  stockMapper.insertStock(stock);
       //获取产品信息
       Product product = productMapper.selectProductById(stock.getPid());
       if(product.getRate()==null){
           product.setRate(BigDecimal.valueOf(1));
       }
       stock.setMoneysum(product.getRate().multiply(new java.math.BigDecimal(stock.getStockmount())));
       stockMapper.updateStock(stock);
       //获取库存信息
       Repertory repertory = repertoryMapper.selectRepertoryById(stock.getPid());
       //库存数量
       Long oldRepertory = repertory.getStorage();
       //进货数量
       Long StockMount = stock.getStockmount();
       //更新库存
       repertory.setStorage(oldRepertory+StockMount);
       repertoryMapper.updateRepertory(repertory);

       return result;
    }

    /**
     * 修改进货信息
     * 
     * @param stock 进货信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateStock(Stock stock)
    {
//        int result = stockMapper.updateStock(stock);
//        //修改后的数量
//        Long updatemount = stock.getStockmount();
//        //获取进货表里原本的数量
//        Stock oldstock =  selectStockById(stock.getPid());//通过修改的id查询 原有的数量
//        Long stockmount = oldstock.getStockmount();
//
//        Long chazhi;
//        //获取库存信息
//        Repertory repertory = repertoryMapper.selectRepertoryById(stock.getPid());
//
//        //变化的数量
//        if(updatemount == stockmount){
//            return 0;
//        }else if(updatemount<stockmount){
//            chazhi = updatemount-stockmount;
//            repertory.setStorage(repertory.getStorage()-chazhi);//退货
//            repertoryMapper.updateRepertory(repertory);
//        }else{
//            chazhi = updatemount-stockmount;
//            repertory.setStorage(repertory.getStorage()+chazhi);//进货
//            repertoryMapper.updateRepertory(repertory);
//        }
        return stockMapper.updateStock(stock);
    }

    /**
     * 批量删除进货信息
     * 
     * @param ids 需要删除的进货信息主键
     * @return 结果
     */
    @Override
    public int deleteStockByIds(Long[] ids)
    {
        return stockMapper.deleteStockByIds(ids);
    }

    /**
     * 删除进货信息信息
     * 
     * @param id 进货信息主键
     * @return 结果
     */
    @Override
    public int deleteStockById(Long id)
    {
        return stockMapper.deleteStockById(id);
    }
}
